﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Smart.Service.ServiceReference;
using System.ServiceModel;

namespace Smart.Service
{
    public abstract class AbstractServiceCommand : IServiceCommand
    {
        /// <summary>
        /// 命名服务
        /// </summary>
        public string Service { get; set; }
        /// <summary>
        /// 解析方式
        /// </summary>
        public string Parser { get; set; }
        /// <summary>
        /// 命名方法
        /// </summary>
        public string Method { get; set; }
        /// <summary>
        /// 请求参数
        /// </summary>
        public IServiceRequest Request { get; set; }
        /// <summary>
        /// 绑定频道
        /// </summary>
        public System.ServiceModel.Channels.Binding Binding { get; set; }
        /// <summary>
        /// 终结点地址
        /// </summary>
        public string EndpointAddress { get; set; }
        /// <summary>
        /// 完成回调方式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public abstract void CompletedCallback(object sender, ExecuteMethodCompletedEventArgs e);

        /// <summary>
        /// 默认终结点
        /// </summary>
        protected static string DefaultEndpointAddressPath
        {
            get
            {
                var host = Application.Current.Host.Source.ToString().Replace(Application.Current.Host.Source.LocalPath, string.Empty);
                return string.Format("{0}/{1}", host, "Service/HttpService.svc");
            }
        }

        /// <summary>
        /// 客户端调用实例
        /// </summary>
        /// <returns></returns>
        protected virtual HttpServiceClient CreateClientInstance()
        {
            var binding = Binding ?? new BasicHttpBinding();
            var endPoint = new EndpointAddress(string.IsNullOrEmpty(EndpointAddress) ? DefaultEndpointAddressPath : EndpointAddress);
            return new HttpServiceClient(binding, endPoint);
        }

        /// <summary>
        /// ICommand 实现 
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public virtual bool CanExecute(object parameter)
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(parameter, null);
            }

            return true;
        }
        /// <summary>
        /// ICommand 实现 
        /// </summary>
        public event EventHandler CanExecuteChanged;

        /// <summary>
        /// ICommand 实现 
        /// </summary>
        /// <param name="parameter"></param>
        public virtual void Execute(object parameter)
        {
            var client = CreateClientInstance();
            client.ExecuteMethodCompleted += CompletedCallback;
            client.ExecuteMethodAsync(Service, Parser, Method, Request.ToXmlString(), parameter);
        }
    }
}
